//
//  ViewController.m
//  MJDemo
//
//  Created by admin on 2021/7/14.
//

#import "BasicViewController.h"

@interface BasicViewController ()<EmptyViewDelegate>

@end

@implementation BasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBackItemAppearance];
    [self mj_setUI];
}

- (void)setBackItemAppearance {
    NSArray *arrViewControllers = self.navigationController.viewControllers;
    if ([arrViewControllers indexOfObject:self] > 0 || self.isShowBack)
    {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        [btn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(handleBackButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
        self.btnBack = btn;
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects: leftItem, nil];
    }
    self.rt_disableInteractivePop = false;//更换了leftBarButtonItems后 需要设置 rt_disableInteractivePop 为 false
}

- (void)handleBackButtonClick{
    [self mj_popViewController];
}

- (void)pushViewController:(NSString *)targetStr{
    UIViewController *VC = [[NSClassFromString(targetStr) alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

/**
 设置导航栏右边文字按钮
 
 @param title 标题
 */
- (void)setNavRightItemTitle:(NSString *)title{
    [self setNavRightItemTitle:title titleColor:nil];
}

- (void)setNavRightItemTitle:(NSString *)title titleColor:(UIColor *)titleColor {
    if(!_btnItemRight){
        _btnItemRight = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(handleRightBarButtonClick)];
    }
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:MJUIFontMake(17)}];
    if (titleColor){
        attrs[NSForegroundColorAttributeName] = titleColor;
    }else{
        attrs[NSForegroundColorAttributeName] = UIColor.mj_appColor;
    }
    [_btnItemRight setTitleTextAttributes:attrs forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = _btnItemRight;
}

/**
 右侧导航栏标题按钮点击
 */
- (void)handleRightBarButtonClick {
    
}

- (void)setNavRightItemTitle:(NSString *)title titleColor:(UIColor *)titleColor disabledColor:(UIColor *)disabledColor{
    
    if(!_btnItemRight){
        _btnItemRight = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(handleRightBarButtonClick)];
    }
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:MJUIFontMake(16)}];
    if (titleColor){
        attrs[NSForegroundColorAttributeName] = titleColor;
    }else{
        attrs[NSForegroundColorAttributeName] = UIColor.mj_blackColor;
    }
    [_btnItemRight setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [_btnItemRight setTitleTextAttributes:[NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:MJUIFontMake(16),NSForegroundColorAttributeName:disabledColor}] forState:UIControlStateDisabled];
    self.navigationItem.rightBarButtonItem = _btnItemRight;
}

/**
 设置导航栏右边图片按钮

 @param name 图片名称
 */
- (void)setNavRightItemImageName:(NSString *)name{
    UIImage *rightImage = [UIImage imageNamed:name];
    if(_btnRight == nil){
        _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnRight addTarget:self action:@selector(handleRightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_btnRight setImage:rightImage forState:UIControlStateNormal];
        _btnRight.frame = CGRectMake(0, 0, 40, 40);
        [_btnRight setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: rightItem, nil];
    }
}

- (void)setNavRightItemImage:(UIImage *)image{
    UIImage *rightImage = image;
    if(_btnRight == nil){
        _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnRight addTarget:self action:@selector(handleRightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_btnRight setImage:rightImage forState:UIControlStateNormal];
        _btnRight.frame = CGRectMake(0, 0, 40, 40);
        [_btnRight setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: rightItem, nil];
    }
}

- (void)setNavRightItemNormalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(handleRightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _btnItemRight = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = _btnItemRight;
}

- (void)setNavLeftItemTitle:(NSString *)title titleColor:(UIColor *)titleColor {
    if(!_btnItemLeft){
        _btnItemLeft = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(handleLeftBarButtonClick)];
    }
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:MJUIFontMake(16)}];
    if (titleColor){
        attrs[NSForegroundColorAttributeName] = titleColor;
    }else{
        attrs[NSForegroundColorAttributeName] = UIColor.mj_blackColor;
    }
    [_btnItemLeft setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [_btnItemLeft setTitleTextAttributes:attrs forState:UIControlStateDisabled];
    self.navigationItem.leftBarButtonItem = _btnItemLeft;
}

/// 左侧导航栏标题按钮点击
- (void)handleLeftBarButtonClick{
    
}

/// 控制器释放打印
- (void)dealloc{
    NSLog(@"[%@]=========已被释放",NSStringFromClass([self class]));
}

/// 设置UI界面样色
- (void)mj_setUI{
    
    //背景颜色
    UIColor *navBackgroundColor = UIColor.mj_whiteColor;
    if(self.navType == NavTypeWhite){
        navBackgroundColor = UIColor.mj_whiteColor;
    }else if(self.navType == NavTypeGray){
        navBackgroundColor = UIColor.mj_backgroundColor;
    }
    
    //
    UIColor *backgroundColorType = UIColor.mj_whiteColor;
    if(self.backgroundColorType == BackgroundColorTypeWhite){
        backgroundColorType = UIColor.mj_whiteColor;
    }else if(self.backgroundColorType == BackgroundColorTypeGray){
        backgroundColorType = UIColor.mj_backgroundColor;
    }
    
    self.view.backgroundColor = backgroundColorType;
    //修改导航栏背景色
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = MJHelper.shared.navBarApp;
        [barApp configureWithOpaqueBackground];
        barApp.backgroundColor = navBackgroundColor;
        barApp.shadowColor = [UIColor clearColor];//隐藏阴影颜色（底部分割线）
        UINavigationBar *navBar = self.navigationController.navigationBar;
        navBar.standardAppearance = barApp;
        navBar.scrollEdgeAppearance = barApp;
    }else{
        //self.rt_navigationController
        [self.navigationController.navigationBar setBackgroundImage:[UIImage
        mj_imageWithColor:navBackgroundColor] forBarMetrics:UIBarMetricsDefault];
    }
    //导航栏底部阴影
    BOOL isDropShadow = false;
    if(isDropShadow){
        [self.navigationController.navigationBar mj_dropShadowWithOffset:CGSizeMake(0, 2) radius:2
                                                          color:[UIColor mj_colorWithHexString:@"#E6E6E6"]
                                                          opacity:0.8];
    }
}

/// 显示空白页面
- (void)showEmptyView:(UIView *)view emptyStyle:(EMPTYSTYLE)emptyStyle{
    
    for (UIView *subView in [view subviews]) {
        if([subView isKindOfClass:[EmptyView class]]){
            return;
        }
    }
    EmptyView *emptyView = [[EmptyView alloc] init];
    CGRect emptyViewFrame = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
    UIImage *image = [UIImage imageNamed:@"暂无咨询"];
    NSString *title = @"暂无数据";
    //NSLog(@"frame111 ===  %@",NSStringFromCGRect(view.bounds));
    NSString *buttonTitle = nil;
    if(emptyStyle == EmptyViewMember)
    {
        image = [UIImage imageNamed:@"bg_meiren"];
        title = @"Members not added. Please add members to continue.";
        buttonTitle = @"Add Member";
    }
    if(emptyStyle == EmptyViewSelectMember)
    {
        image = [UIImage imageNamed:@"bg_meiren"];
        title = @"Members not added. Please add members to continue.";
    }
    if(emptyStyle == EmptyViewRecord)
    {
        image = [UIImage imageNamed:@"bg_meijiance"];
        title = @"Records empty. No results to display.";
    }
    if(emptyStyle == EmptyViewSearchArchive)
    {
        image = [UIImage imageNamed:@"bg_zhaobudaoren"];
        title = @"No results found.";
    }
    if(emptyStyle == EmptyViewMemberInfo)
    {
        image = [UIImage imageNamed:@"bg_meijiance"];
        title = @"Tests have not started yet.";
    }
    if(emptyStyle == EmptyViewCountryCode)
    {
        image = [UIImage imageNamed:@"bg_nofoundcode"];
        title = @"No results found country";
    }
    
    emptyView.frame = emptyViewFrame;
    emptyView.delegate = self;
    [emptyView showEmptyViewWithImage:image Title:title DetailTitle:nil buttonTitle:buttonTitle withView:view emptyStyle:emptyStyle];
}

- (void)handleEmptyButtonClick:(UIButton *)sender{
    
}

/// 隐藏空白页面
- (void)hideEmptyView:(UIView *)view {
    [EmptyView hideEmptyView:view];
}

- (void)setNavRightItemTitle:(NSString *)title imageName:(NSString *)imageName {
    _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight sizeToFit]; //自适应按钮大小***
    [_btnRight setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    _btnRight.titleLabel.font = [UIFont systemFontOfSize:15];
    [_btnRight setTitleColor:UIColor.mj_blackColor forState:0];
    [_btnRight setTitle:title forState:0];
        [_btnRight addTarget:self action:@selector(handleRightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_btnRight mj_layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageLeft imageTitlespace:2];
    _btnRight.titleLabel.textAlignment = NSTextAlignmentRight;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: rightItem, nil];
}

/** pop到指定控制器*/
- (void)popViewControllerWithTargetArr:(NSArray *)targetArr WaitThread:(BOOL)waitThread{
    if(waitThread){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJUIAwaitTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            BOOL isBreak = false;
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if(isBreak){
                    break;
                }
                for (NSString *targetStr in targetArr) {
                    if ([vc isKindOfClass:[NSClassFromString(targetStr) class]]) {
                        [self.navigationController popToViewController:vc animated:YES];
                        isBreak = true;
                        break;
                    }
                }
            }
        });
    }else{
        
        BOOL isBreak = false;
        NSArray* viewControllers = [[self.navigationController.viewControllers reverseObjectEnumerator] allObjects];
        for (UIViewController *vc in viewControllers) {
            if(isBreak){
                break;
            }
            for (NSString *targetStr in targetArr) {
                if ([vc isKindOfClass:[NSClassFromString(targetStr) class]]) {
                    isBreak = true;
                    [self.navigationController popToViewController:vc animated:YES];
                    break;
                }
            }
        }
    }
}

/// 取消向下偏移 64/20
/// @param srcollView 滚动
- (void)setContentInsetNever:(UIScrollView *)srcollView{
    srcollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

- (MJCommonModel *)getParmModel{
    
    BasicViewController *parentViewController = (BasicViewController *)self.parentViewController.parentViewController;
    if([parentViewController isKindOfClass:BasicViewController.class]){
        NSLog(@"MJLog========111===============%@",parentViewController.patientId);
        MJCommonModel *parms = [[MJCommonModel alloc]init];
        parms.patientId = parentViewController.patientId;
        parms.minDate = parentViewController.minDate;
        parms.maxDate = parentViewController.maxDate;
        return parms;
    }else{
        MJCommonModel *parms = [[MJCommonModel alloc]init];
//        parms.patientId = parentViewController.patientId;
//        parms.minDate = parentViewController.minDate;
//        parms.maxDate = parentViewController.maxDate;
        return parms;
    }
    
}
@end
